package com.neusoft.nep.NEPM.controller;

import com.neusoft.nep.entity.MonthData;
import com.neusoft.nep.entity.nepgResult;
import com.neusoft.tools.*;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;

public class AqiTrendTotalStatisViewController {
    @FXML
    private TableView tableview;
    @FXML
    private TreeView treeview;
    @FXML
    private Button btn_out;
    @FXML
    private TreeItem To_AqiTrendTotalStatisView;
    @FXML
    private TreeItem To_publicAqiDataList;
    @FXML
    private TreeItem To_confirmDataView;
    @FXML
    private TreeItem To_AqiSeperatedStatisView;
    @FXML
    private TreeItem To_OtherStatisView;
    @FXML
    private TreeItem To_ProvinceDataTotalStatisView;
    @FXML
    private TableColumn col_month;
    @FXML
    private TableColumn col_number;
    @FXML
    private TableColumn col_cnt;
    @FXML
    private ImageView imageView;
    private ObservableList LastData= FXCollections.observableArrayList();
    @FXML
    public void initialize(){
        treeview.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue == To_AqiSeperatedStatisView) {
                try {
                    FXMLUtils.switchToShow(btn_out,"NEPM/view/AqiSeperatedStatisView.fxml");
                } catch (IOException e) {e.printStackTrace();
                    AlertUtils.showDialog("错误", "页面跳转失败: " + e.getMessage());
                }
            } else if (newValue == To_AqiTrendTotalStatisView) {
                try {
                    FXMLUtils.switchToShow(btn_out,"NEPM/view/AqiTrendTotalStatisView.fxml");
                } catch (IOException e) {
                    e.printStackTrace();
                    AlertUtils.showDialog("错误", "页面跳转失败: " + e.getMessage());
                }
            } else if (newValue == To_OtherStatisView) {
                try {
                    FXMLUtils.switchToShow(btn_out,"NEPM/view/OtherStatisView.fxml");
                } catch (IOException e) {
                    e.printStackTrace();
                    AlertUtils.showDialog("错误", "页面跳转失败: " + e.getMessage());
                }
            } else if (newValue == To_ProvinceDataTotalStatisView) {
                try {
                    FXMLUtils.switchToShow(btn_out,"NEPM/view/ProvinceDataTotalStatisView.fxml");
                } catch (IOException e) {
                    e.printStackTrace();
                    AlertUtils.showDialog("错误", "页面跳转失败: " + e.getMessage());
                }

            }
            else if (newValue == To_publicAqiDataList) {
                try {
                    FXMLUtils.switchToShow(btn_out, "NEPM/view/publicAqiDataList.fxml");
                } catch (IOException e) {
                    e.printStackTrace();
                    AlertUtils.showDialog("错误", "页面跳转失败: " + e.getMessage());
                }
            }
            else if (newValue == To_confirmDataView) {
                try {
                    FXMLUtils.switchToShow(btn_out, "NEPM/view/confirmDataView.fxml");
                } catch (IOException e) {
                    e.printStackTrace();
                    AlertUtils.showDialog("错误", "页面跳转失败: " + e.getMessage());
                }
            }
        });
        Image image=new Image("resource/image/ayajinene.jpg");
        imageView.setImage(image);
        col_month.setCellValueFactory(new PropertyValueFactory<>("date"));
        col_number.setCellValueFactory(new PropertyValueFactory<>("num"));
        col_cnt.setCellValueFactory(new PropertyValueFactory<>("cnt"));
        // 设置表格列宽比例为 3:3:4
        col_number.prefWidthProperty().bind(tableview.widthProperty().multiply(0.3));
        col_month.prefWidthProperty().bind(tableview.widthProperty().multiply(0.3));
        col_cnt.prefWidthProperty().bind(tableview.widthProperty().multiply(0.4));
        Map monthMap=new LinkedHashMap<String, MonthData>();
        List<nepgResult> RecordData=new ArrayList<>();
        LocalDate now=LocalDate.now();
        for(int i=0;i<=11;i++)
        {
            LocalDate date = now.minus(i, ChronoUnit.MONTHS);
            String monthStr = date.format(DateTimeFormatter.ofPattern("yyyy-MM"));
            monthMap.put(monthStr,new MonthData(0,monthStr,i+1));
        }
        RecordData=FileUtils.readListObject("NEPGresult.txt");
        for(nepgResult item:RecordData){
            String[] time = item.getData().split("-");
            String time_year=time[0];
            String time_month=time[1];
            String time_year_month=time_year+'-'+time_month;
            if(monthMap.containsKey(time_year_month)){
                MonthData monthdata= (MonthData) monthMap.get(time_year_month);
                int new_cnt=monthdata.getCnt();
                new_cnt++;
                monthdata.setCnt(new_cnt);
            }
        }
        LastData.clear();
        LastData.addAll(monthMap.values());
        tableview.setItems(LastData);
    }
    public void Log_out() throws IOException {
        FXMLUtils.switchToShow(btn_out,"NEPM/view/LoginView.fxml");
    }
    public void back() throws IOException {
        FXMLUtils.switchToShow(btn_out,"NEPM/view/ManagerUIView.fxml");
    }
}
